package com.thread;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: PrintSortedNums
 * @description: 多线程按顺序输出数字
 * @author: Wang Zhihua
 * @date: 2022-01-13
 */
public class PrintSorted {

    private static final List<MyLock> myLocks = new ArrayList<>();

    public static class MyLock {
        public String awakeThreadName = "";

        public MyLock(String awakeThreadName) {
            this.awakeThreadName = awakeThreadName;
        }
    }

    public static void main(String[] args) {
        print(10);
        synchronized (myLocks.get(0)) {
            myLocks.get(0).notify();
        }
    }

    public static void print(int threadNum) {
        List<Thread> threads = new ArrayList<>(threadNum);

        for (int i = 0; i < threadNum; ++i) {
            MyLock myLock = new MyLock("thread-" + i);
            myLocks.add(myLock);
        }

        for (int i = 0; i < threadNum; ++i) {

            final int x = i;
            threads.add(new Thread() {
                @Override
                public void run() {
                    while (true) {

                    }
                }
            });
        }

        for (int i = 0; i < threadNum; ++i) {
            threads.get(i).start();
        }
    }

}
